package com.sun.umbrella.service.shiro.filter;

import com.sun.umbrella.dao.OauthUserMapper;
import com.sun.umbrella.service.shiro.Constants;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.PathMatchingFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.naming.AuthenticationException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author Jay
 * @version v1.0
 * @description 过滤器
 * @date 2017-11-18 22:53
 */
public class UserFilter  extends PathMatchingFilter {


    @Autowired
    private OauthUserMapper oauthUserMapper;

    @Override
    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        if(StringUtils.isEmpty(username)){
            throw new AuthenticationException("userName is null");
        }
        request.setAttribute(Constants.CURRENT_USER, oauthUserMapper.selectByUserName(username));
        return true;
    }
}
